<template>
	<view class="content">
		<view style="height: 100rpx;"></view>
		<view style="width: 90%;position: fixed;top: 100rpx;">
			<uni-icons @click="back" type="back" size="30" style="color: #000;"></uni-icons>
		</view>
		<view style="height: 100rpx;"></view>

		<image style="width: 150rpx;height: 150rpx;border-radius: 50%;margin-top: 80rpx;" src="/static/logo.png"
			mode="aspectFill"></image>

		<view style="font-size: 35rpx;font-weight: bold;margin-top: 40rpx;color: #000;">{{id ==0 ? '手机验证码登录' : '邮箱验证码登录'}}</view>
		<!-- 		<view style="font-size: 25rpx;color: #999999;">未注册的手机号, 将为您自动注册</view> -->

		<view
			style="width: 80%;display: flex;justify-content: center;background-color: rgba(225, 225, 225, .4);border-radius: 50rpx;margin-top: 40rpx;">
			<input v-model="form.user" style="width: 90%;padding: 30rpx 0;color: #000;" :placeholder="id==0 ? '请输入手机号' : '请输入邮箱'"
				placeholder-class="pcls" />
		</view>

		<view
			style="width: 80%;border-radius: 10rpx;background-color: rgba(225, 225, 225, .4);display: flex;align-items: center;border-radius: 50rpx;margin-top: 20rpx">
			<input :placeholder="$t('login.14')" v-model="form.code" placeholder-class="pcls"
				style="padding: 30rpx 20rpx;width: 90%;color: #000;" />
			<view class="puBut3" @click="send" v-if="codeFalg"
				style="padding: 10rpx;width:150rpx;text-align: center;margin-right: 20rpx;border-radius: 50rpx;font-size: 25rpx;">
				{{$t('login.15')}}
			</view>
			<view class="puBut9" v-else
				style="padding: 10rpx;width:150rpx;text-align: center;margin-right: 20rpx; border-radius: 50rpx;font-size: 25rpx;">
				{{countDown}}S
			</view>
		</view>

		<view @click="register" style="width: 80%;margin-top: 80rpx;">
			<button class="puBut3" style="width: 100%;font-size: 30rpx;border-radius: 40rpx;color: #fff;">登录</button>
		</view>



		<view style="width: 80%;display: flex;margin-top: 80rpx;">
			<view>
				<radio style="transform: scale(0.7);" @click="radioFalg ? radioFalg = false : radioFalg = true" value=""
					:checked="radioFalg" :color="'#06DB9A'" />
			</view>
			<view style="font-size: 25rpx;color: #000;">{{$t(('login.8'))}},《<span style="color: #06DB9A;font-weight: bold;"
					@click="agreement">{{$t('login.9')}}</span>》{{$t('login.10')}} 《<span
					style="color: #06DB9A;font-weight: bold;"
					@click="privacy">{{$t('login.11')}}</span>》，{{$t('login.12')}}</view>
		</view>
	</view>
</template>

<script>
	import {
		encryption
	} from "@/static/js/aesEncrypt.js"
	import methods from "@/utils/methods.js"
	import main from "../../utils/main.js"
	export default {
		data() {
			return {
				id: 0,
				form: {
					user: '',
					code: ''
				},
				radioFalg: false,
				codeFalg: true,
				countDown: 60,
			}
		},
		onLoad(data) {
			if (data.id) {
				this.id = data.id
			}
			const phone = uni.getStorageSync('phone');
			if (phone) {
				this.form.user = phone
			}
		},
		watch: {
			countDown(newVal, oldVal) {
				//console.log('newVal:'+newVal)
				if (newVal == 0) {
					clearInterval(this.codeDsObj)
					this.codeDsObj = null
					this.codeFalg = true
				}
			}
		},
		mounted() {

		},
		methods: {
			agreement() {

			},
			privacy() {

			},
			register() {
				if (!this.radioFalg) {
					uni.showToast({
						icon: 'none',
						title: this.$t('common.29')
					})
					return
				}
				if (!this.form.code) {
					uni.showToast({
						icon: 'none',
						title: this.$t('login.14')
					})
					return
				}
				
				if(this.id == 0){
					if (!this.form.user) {
						uni.showToast({
							icon: 'none',
							title: this.$t('common.30')
						})
						return false
					} else {
						var pattern = /^1[03456789]\d{9}$/;
						let falg = pattern.test(this.form.user);
						if (!falg) {
							uni.showToast({
								icon: 'none',
								title: this.$t('common.31')
							})
							return false
						}
					}
					methods.getEncryptedString().then(res => {
						if (res.data.code == 200) {
							if (res.data.data) {
								//加密用户手机号
								const maxUserIphone = encryption(this.form.user, res.data.data.key, res.data.data.iv);
								let obj = {
									phone: maxUserIphone,
									password: '',
									code: this.form.code
								}
								methods.register(obj).then(res => {
									if (res.data.code == 200) {
										if (res.data.data) {
											uni.setStorageSync('token', res.data.data.info)
											uni.setStorageSync('phone', this.form.user)
											// main.getcid()
											// this.$store.dispatch('time')
											// if (!this.$store.state.isOpen) {
											// 	this.$store.dispatch('link')
											// }
											uni.showToast({
												icon: 'none',
												title: res.data.message
											})
					
											setTimeout(() => {
												uni.reLaunch({
													url: '/pages/index/index'
												})
											}, 1000)
										}
									}
								}).catch(err => {
									uni.showToast({
										icon: 'none',
										title: err.data.message
									})
								})
							}
						}
					})
				}else{
					if (!this.form.user) {
						uni.showToast({
							icon: 'none',
							title: this.$t('common.40')
						})
						return false
					} else {
						let re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
						let falg = re.test(this.form.user);
						if (!falg) {
							uni.showToast({
								icon: 'none',
								title: this.$t('common.39')
							})
							return false
						}
					}
					
					methods.getEncryptedString().then(res => {
						if (res.data.code == 200) {
							if (res.data.data) {
								//加密用户密码
								let obj = {
									email: this.form.user,
									code: this.form.code
								}
								methods.registerByEmail(obj).then(res => {
									if (res.data.code == 200) {
										if (res.data.data) {
											uni.setStorageSync('token', res.data.data.info)
											uni.setStorageSync('email', this.form.user)
											uni.setStorageSync('pass', this.form.pass)
											// this.$store.dispatch('time')
											uni.showToast({
												icon: 'none',
												title: res.data.message
											})
											uni.reLaunch({
												url: '/pages/index/index'
											})
										}
									}
								}).catch(err => {
									uni.showToast({
										icon: 'none',
										title: err.data.message
									})
								})
							}
						}
					})
				}

				

			},
			send() {
				let self = this
				if (!self.radioFalg) {
					uni.showToast({
						icon: 'none',
						title: self.$t('common.29')
					})
					return
				}
				if (self.id == 0) {
					if (!self.form.user) {
						uni.showToast({
							icon: 'none',
							title: self.$t('common.30')
						})
						return false
					} else {
						var pattern = /^1[03456789]\d{9}$/;
						let falg = pattern.test(self.form.user);
						if (!falg) {
							uni.showToast({
								icon: 'none',
								title: self.$t('common.31')
							})
							return false
						}
					}
					uni.showLoading()
					methods.getEncryptedString().then(res => {
						uni.hideLoading()
						if (res.data.code == 200) {
							if (res.data.data) {
								//加密用户手机号
								const maxUserIphone = encryption(self.form.user, res.data.data.key, res.data.data
									.iv);
								//加密用户密码
								//const maxPassword = encryption(this.password, res.data.data.key, res.data.data.iv);
								let obj = {
									phone: maxUserIphone,
									password: '',
									code: ''
								}
								methods.register(obj).then(res => {
									if (res.data.code == 100) {
										self.countDown = 60
										self.codeFalg = false

										self.codeDsObj = setInterval(() => {
											self.countDown -= 1
										}, 1000)
										uni.showToast({
											icon: 'none',
											title: res.data.message
										})
									}
								}).catch(() => {
									uni.hideLoading()
								})
							}
						}
					})
				} else {
					if (!self.form.user) {
						uni.showToast({
							icon: 'none',
							title: self.$t('common.40')
						})
						return false
					} else {
						let re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
						let falg = re.test(self.form.user);
						if (!falg) {
							uni.showToast({
								icon: 'none',
								title: self.$t('common.39')
							})
							return false
						}
					}
					uni.showLoading()
					let obj = {
						email: self.form.user,
						code: ''
					}
					methods.registerByEmail(obj).then(res => {
						uni.hideLoading()
						if (res.data.code == 100) {
							self.countDown = 60
							self.codeFalg = false

							self.codeDsObj = setInterval(() => {
								self.countDown -= 1
							}, 1000)
							uni.showToast({
								icon: 'none',
								duration: 3000,
								title: res.data.message
							})
						}
					}).catch(() => {
						uni.hideLoading()
					})
				}



			},
			back() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		min-height: 100vh;
		background: linear-gradient(to bottom, #DFD7F9 0px, #f2f0f9 398rpx, #F5F6F8 100%)
	}

	.pcls {
		font-size: 25rpx;
		color: #000;
	}
</style>